// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Nasty seek & destroy patrol script from scratch
// -----------------------------------------------

private [
	"_areasize",
	"_alert",
	"_cible",
	"_cibles",
	"_deadcounter",
	"_formationtype",
	"_group",
	"_leader",
	"_lastposition",
	"_list",
	"_marker",
	"_move",
	"_position",
	"_originalsize",
	"_enemyside",
	"_wp",
	"_wptype"
];

_group    = _this select 0;
_position = _this select 1;
_areasize = _this select 2;

if (isNil "_areasize") exitWith {
	hintC "WARCONTEXT: patrolscript: areasize parameter is not set";
};

_leader = leader _group;
if ((side _leader) in wcside) then {
	_enemyside = wcenemyside;
} else {
	_enemyside = wcside;
};

if (_group in wcpatrolgroups) exitWith {};

_marker = [format ["patrolzone%1", wcpatrolindex], _areasize, _position, "ColorRed", "ELLIPSE", "Border", "", 0, "", false] call WC_fnc_createmarkerlocal;
wcpatrolindex = wcpatrolindex + 1;

wcpatrolgroups = wcpatrolgroups + [_group];

_originalsize = count (units _group);
_lastposition = position (leader _group);

_move = true;
_deadcounter = 0;
_alert = false;

{
	_x setVariable ["destination", [0,0,0], false];
} foreach (units _group);

while {(count (units _group) > 0)} do {
	_leader = leader _group;

	// Check if someone has been kill recently in the group
	if (_originalsize < count (units _group)) then {
		_alert = true;
		_deadcounter = _deadcounter + 1;
		if (_deadcounter > 20) then {
			_originalsize = count (units _group);
			_deadcounter = 0;
			_alert = false;
		};
	};

	if ((wcalert > 50) or _alert) then {
		_group setBehaviour "AWARE";
		_group setCombatMode "RED";

		_cibles = [];

		_list = (getMarkerPos _marker) nearEntities [["Man"], _areasize];
		if (count _list > 0) then {
			{
				if (side _x in _enemyside) then {
					if (_x distance (_leader getHideFrom _x) < 100) then {
						_cibles = _cibles + [_x];
					};
				} else {
					_list = _list - [_x];
				};
				sleep 0.1;
			} foreach _list;

			if (count _cibles == 0) then {
				_group setBehaviour "SAFE";
			} else {
				{
					_cible = (([_x, _cibles] call EXT_fnc_SortByDistance) select 0);
					_x setVariable ["cible", _cible, false];
					_x doTarget _cible;
					if ((_x getVariable "destination") distance (position _cible) > 10) then {
						_position = ([position _cible, 3, 360, getDir _cible, 10] call WC_fnc_createcircleposition) call BIS_fnc_selectRandom;
						_x setVariable ["destination", _position, false];
						_x doMove _position;
					};
					_x doFire _cible;
					sleep 0.1;
				} foreach (units _group);
			};
		};
		sleep 30;
	} else {
		if (count (units _group) > 2) then {
			_wptype = ["MOVE", "SAD"];
		} else {
			_wptype = ["MOVE", "SAD", "HOLD", "SENTRY"];
		};

		_group setBehaviour "SAFE";
		_group setCombatMode "GREEN";

		_formationtype = ["COLUMN","STAG COLUMN","WEDGE","ECH LEFT","ECH RIGHT","VEE","LINE","FILE","DIAMOND"] call BIS_fnc_selectRandom;
		_group setFormation _formationtype;

		_position = [_marker, "onground"] call WC_fnc_createpositioninmarker;

		_wp = _group addWaypoint [_position, 0];
		_wp setWaypointPosition [_position, 5];
		_wp setWaypointType (_wptype call BIS_fnc_selectRandom);
		_wp setWaypointSpeed "LIMITED";
		_group setCurrentWaypoint _wp;

		_move = false;
		while {((wcalert < 50) and !(_move) and (count (units _group) == _originalsize))} do {
			_lastposition = position (leader _group);
			sleep 10;
			if (_lastposition distance (position (leader _group)) < 5) then {
				_move = true;
			};
		};
		deleteWaypoint _wp;
	};
	sleep 0.1;
};

deleteMarkerLocal _marker;
wcpatrolgroups = wcpatrolgroups - [_group];